package com.fh.controller.customer;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fh.controller.base.BaseController;
import com.fh.entity.CustomerEntity;
import com.fh.entity.Page;
import com.fh.entity.system.User;
import com.fh.service.customer.CustomerService;
import com.fh.util.Const;
import com.fh.util.PageData;

/** 
 * @ClassName:     Order 客户管理
 * @Description:TODO(这里用一句话描述这个类的作用) 
 * @author:    冯辉 
 * @date:        2017-3-29 下午6:24:26 
 *  
 */
@Controller
@RequestMapping(value = "/customer")
public class Customer extends BaseController {
   
	@Resource(name="customerService")
	private CustomerService customerService;
	
	/**
	 * 跳转到客户管理页面
	 * @return
	 */
	@RequestMapping(value="/customerlist")
	public String queryCustomer(){
		return "customer/customerlist";
	}
	/**
	 * 根据供应商id查询顾客列表
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping(value="/queryCustomerList")
	@ResponseBody
	public Object queryCustomerList(HttpServletRequest request,int page,int rows) throws Exception{
		String star=request.getParameter("star");//星级
		String name=request.getParameter("name");//顾客名字
		String times=request.getParameter("times");//交易次数
		String amount=request.getParameter("amount");//交易额
		String startTime=request.getParameter("startTime");//开始时间
		String endTime=request.getParameter("endTime");//结束时间
		
		//从session中取出供应商信息
		Subject currentUser = SecurityUtils.getSubject();  
		Session session = currentUser.getSession();
		User user = (User)session.getAttribute(Const.SESSION_USER);
			//分页对象
			Page pd = new Page();
			//设置参数
			PageData pda = new PageData();
			pda=this.getPageData();
			pda.put("id", user.getSupplierid());
			pda.put("star", star);
			pda.put("name", name);
			pda.put("times", times);
			pda.put("amount", amount);
			pda.put("startTime", startTime);
			pda.put("endTime", endTime);
			pd.setPd(pda);
			//客户列表总条数
		    int totalCount=customerService.queryCustomerCount(pd);
			pd.setPageParams(totalCount, rows, page);
			//订单列表
			List<CustomerEntity> list=customerService.queryCustomerList(pd);
			//把结果集和总条数返回
		    Map<String,Object> map=new HashMap<String,Object>();
		    map.put("total", totalCount);
		    map.put("rows", list);
			return map;
	}
	/**
	 * 根据顾客id查询顾客详情
	 * @return
	 */
	@RequestMapping(value="/customerDetail")
	public String customerDetail(String id,Model model){
		//从session中取出供应商信息
		Subject currentUser = SecurityUtils.getSubject();  
		Session session = currentUser.getSession();
		User user = (User)session.getAttribute(Const.SESSION_USER);
		PageData pde = new PageData();
		pde.put("id", id);
		pde.put("supplierId", user.getSupplierid());
		try {
			CustomerEntity customer =customerService.findCustomerInfoById(pde);
			model.addAttribute("customer", customer);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "customer/customerdetail";
	}
	/**
	 * 根据顾客id查询顾客头像
	 */
	@RequestMapping(value="/queryPic")
	public String queryPic(String id,Model model){
		try {
			CustomerEntity pic= customerService.queryPic(id);
			model.addAttribute("pic", pic);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "customer/customerPic";
	}
	
}
